﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using WxYygh.Common;
using WxYygh.Models;
using WxYygh.Models.WeChatPay;
using WxYygh.ServiceReference2;
using WxYygh.ServiceReference1;

namespace WxYygh.Controllers
{
    [WeChatCheck]
    public class YyghController : Controller
    {
        public static string bx_success = "8gZtGKdOtBp6AMB7mcd9hiYpRes1UvDw92DKD6-IXRg";
        public static string bx_cancel = "tF9Zc90K4eCLevQQ-sGZLo5bi1mmlh03GiBZ6kfNB-0";
        // GET: Yygh
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MyYygh()
        {
            //RefundFee("150698661120190409154223427");
            string sql = string.Format(@"select sfzh from GZH_BrInfo where openid ='{0}'", Session["openid"].ToString());

            DataSet dt = DbHelperSQL.Query(sql);
            string sfzh = string.Empty;

            for (int i = 0; i < dt.Tables[0].Rows.Count; i++) {
                if (i == 0 && dt.Tables[0].Rows.Count>1)
                {
                    sfzh +=  dt.Tables[0].Rows[i]["sfzh"].ToString() + "',";
                }
                else if (dt.Tables[0].Rows.Count==1)
                {
                    sfzh += dt.Tables[0].Rows[i]["sfzh"].ToString() ;
                }
                else if (i == dt.Tables[0].Rows.Count - 1)
                {
                    sfzh += "'" + dt.Tables[0].Rows[i]["sfzh"].ToString();
                }
                else {
                    sfzh += "'" + dt.Tables[0].Rows[i]["sfzh"].ToString() + "',";
                }
               
            }

          
            List<YYXXModel> list = getyyxx(sfzh);

            ViewBag.all = list.OrderByDescending(x => x.YYXH).ToList();
            ViewBag.wqh = list.Where(x => x.YYBZ == "待取号").ToList().OrderByDescending(x => x.YYXH).ToList();
            ViewBag.qt = list.Where(x => x.YYBZ != "待取号").ToList().OrderByDescending(x => x.YYXH).ToList();

            return View();
        }

        public ActionResult DoctDetail(string ysdm)
        {

            doctInfo ysxx = DbHelperSQL.Query("select * from doct_info where doctid ='" + ysdm + "'").Tables[0].ToDataDto<doctInfo>();

            ViewBag.ysxx = ysxx;
            return View();
        }
        /// <summary>
        /// 预约详情
        /// </summary>
        /// <returns></returns>
        public ActionResult YyDetail(string hyid,string pzhm)
        {
            string sql = string.Format(@"select sfzh from GZH_BrInfo where openid ='{0}'", Session["openid"].ToString());

            DataSet dt = DbHelperSQL.Query(sql);
            string sfzh = string.Empty;

            for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
            {
                if (i == 0 && dt.Tables[0].Rows.Count > 1)
                {
                    sfzh += dt.Tables[0].Rows[i]["sfzh"].ToString() + "',";
                }
                else if (dt.Tables[0].Rows.Count == 1)
                {
                    sfzh += dt.Tables[0].Rows[i]["sfzh"].ToString();
                }
                else if (i == dt.Tables[0].Rows.Count - 1)
                {
                    sfzh += "'" + dt.Tables[0].Rows[i]["sfzh"].ToString();
                }
                else
                {
                    sfzh += "'" + dt.Tables[0].Rows[i]["sfzh"].ToString() + "',";
                }

            }
            ViewBag.yyxx = getyyxx(sfzh).Where(x => x.HYID == hyid && x.PZHM==pzhm).ToList();

            sql = string.Format(@"select * from ms_yygh where hyid='{0}' and pzhm='{1}'", hyid, pzhm);

            dt = DbHelperSQL.Query(sql);


            if (dt.Tables[0].Rows.Count > 0) {
                ViewBag.out_trade_no = dt.Tables[0].Rows[0]["out_trade_no"].ToString();
                ViewBag.time = DateTime.Parse(dt.Tables[0].Rows[0]["add_time"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            } else {
                ViewBag.out_trade_no = "";
                ViewBag.time = "";
            }

            return View();

        }

        public ActionResult DoctInfo(string ksdm, string ksmc)
        {
            ViewBag.ksdm = ksdm;
            ViewBag.ksmc = ksmc;
            ViewBag.Title = ksmc;

            return View();
        }


        public string getDoctInfo(string ksdm)
        {
          
            List<isyyrs> zjys = new List<isyyrs>();
            string isptys = "0";
            List<KSPBResult> list = getyspb(ksdm, "", DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));

            List<KSPBResult> yspb = list.Where((x, i) => list.FindIndex(z => z.YSDM == x.YSDM) == i).ToList().OrderBy(a => a.YSDM).ToList().Where(b => b.YSDM != "-1").ToList();

            if (yspb.Count > 0)
            {
                for (int i = 0; i < yspb.Count; i++)
                {
                    isyyrs model = new isyyrs();
                    model.ysdm = yspb[i].YSDM;
                    model.ysxm = yspb[i].YSXM;

                    DataTable dt = DbHelperSQL.Query("select goodat from doct_info where doctid ='" + yspb[i].YSDM + "'").Tables[0];
                    model.ysjs = dt.Rows.Count > 0 ? dt.Rows[0]["goodat"].ToString() : "暂无介绍";
                    if (list.Where(x => x.YSDM == yspb[i].YSDM && int.Parse(x.KGRS) > 0).Count() > 0)
                    {
                        model.isky = "1";
                    }
                    else
                    {
                        model.isky = "0";
                    }
                    zjys.Add(model);
                }
            }
            if (list.Where(x => x.YSDM == "-1" && int.Parse(x.KGRS) > 0).Count() > 0)
            {
                isptys = "1";
            }
           

            JsonValues json = new JsonValues();

            json.data = zjys;
            json.value = isptys;

            return json.ToJson();
        }

        public ActionResult Yspb(string ksdm, string ysdm,string ksmc)
        {
            ViewBag.Title = "医生";

            doctInfo ysxx = DbHelperSQL.Query("select * from doct_info where doctid ='" + ysdm + "'").Tables[0].ToDataDto<doctInfo>();

            ViewBag.ysxx = ysxx;
            ViewBag.rq = getdate(0);

            List<KSPBResult> list = getyspb(ksdm, ysdm, DateTime.Now.AddDays(0).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(7).ToString("yyyy-MM-dd"));

            Session["list"] = list;

            ViewBag.list = list;
            ViewBag.ksmc = ksmc;

            return View();
        }

        public ActionResult SelHyxx(string pbid, string brxm, string sfzh)
        {
            ViewBag.Title = "确认信息";
            string sql = string.Empty;
            if (string.IsNullOrEmpty(sfzh))
            {
                sql = string.Format(@"select * from GZH_BrInfo where openid ='{0}' and isdefault=1", Session["openid"].ToString());
            }
            else
            {
                sql = string.Format(@"select * from GZH_BrInfo where openid ='{0}' and sfzh ='{1}'", Session["openid"].ToString(), sfzh);
            }


            DataSet dt = DbHelperSQL.Query(sql);

            UserModel user = dt.Tables[0].ToDataDto<UserModel>();

            ViewBag.user = user;
          
            KSPBResult model = ((List<KSPBResult>)(Session["list"])).Where(x => x.PBID == pbid).ToList()[0];
            if (model.ZBLB == "1" || model.ZBLB == "上午")
            {
                model.ZBLB = "上午";
            }
            else {
                model.ZBLB = "下午";
            }
          
            ViewBag.model = model;

            WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
            string strXml = "<?xml version='1.0' encoding='utf-8'?>";

            strXml = "<?xml version='1.0' encoding='utf-8'?>";
            strXml += "<data>";
            strXml += "<funcode>100203</funcode>";
            strXml += "<PBID>" + pbid + "</PBID>";
            strXml += "<YYLY>3</YYLY>";
            strXml += "</data>";

            string outputXml = "";
            int refxml = hydservice.MainService(strXml, ref outputXml);



            List<HYXXResult> hyxx = new List<HYXXResult>();

            XmlDocument xDoc2 = new XmlDocument();
            xDoc2.LoadXml(outputXml);
            XmlNodeList nodeList0 = xDoc2.SelectSingleNode("data//list").ChildNodes;
            foreach (XmlNode node in nodeList0)
            {
                HYXXResult model2 = new HYXXResult();
                XmlNodeList nodeList2 = node.ChildNodes;
                foreach (XmlNode node2 in nodeList2)
                {
                    switch (node2.Name)
                    {
                        case "HYID":
                            model2.HYID = node2.InnerText;
                            break;
                        case "GZRQ":
                            model2.GZRQ = node2.InnerText;
                            break;
                        case "JZXH":
                            model2.JZXH = node2.InnerText;
                            break;
                        case "KSSJ":
                            model2.KSSJ = node2.InnerText;
                            break;
                        case "KSDM":
                            model2.KSDM = node2.InnerText;
                            break;
                        case "HYZT":
                            model2.HYZT = node2.InnerText;
                            break;
                        default:
                            break;

                    }

                }
                hyxx.Add(model2);
            }
            hyxx = hyxx.Where(x => x.HYZT == "0").ToList();

            ViewBag.hyxx = hyxx;

            return View();
        }

        public ActionResult YyRole()
        {

            ViewBag.Title = "平台预约挂号规则";
            return View();
        }
        /// <summary>
        /// 获取日期
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public List<rqdate> getdate(int n)
        {
            List<rqdate> list = new List<rqdate>();
            string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            for (int i = n; i < 8; i++)
            {
                rqdate model = new rqdate();
                model.ymd = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                model.rq = DateTime.Now.AddDays(i).ToString("MM-dd");
                model.ts = Day[Convert.ToInt32(DateTime.Now.AddDays(i).DayOfWeek.ToString("d"))].ToString();
                list.Add(model);
            }

            return list;
        }
        /// <summary>
        /// 获取医生排班
        /// </summary>
        /// <param name="ksdm"></param>
        /// <param name="kssj"></param>
        /// <param name="jssj"></param>
        /// <returns></returns>
        public List<KSPBResult> getyspb(string ksdm, string ysdm, string kssj, string jssj)
        {

            WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
            string strXml = "<?xml version='1.0' encoding='utf-8'?>";
            strXml += "<data>";
            strXml += "<funcode>100201</funcode>";
            strXml += "<KSDM>" + ksdm + "</KSDM>";
            strXml += "<YSDM>" + ysdm + "</YSDM>";
            strXml += "<YYLY>3</YYLY>";
            strXml += "</data>";

            string outputXml = "";
            int refxml = hydservice.MainService(strXml, ref outputXml);

            List<KSPBResult> kspb = new List<KSPBResult>();

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(outputXml);
            XmlNodeList nodeList = xDoc.SelectSingleNode("data//list").ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                KSPBResult ModelList = new KSPBResult();

                XmlNodeList nodeList2 = node.ChildNodes;
                foreach (XmlNode node2 in nodeList2)
                {
                    switch (node2.Name)
                    {
                        case "GHKS":
                            ModelList.PBID = node2.InnerText;
                            break;
                        case "GZRQ":
                            ModelList.GZRQ = DateTime.Parse(node2.InnerText).ToString("yyyy-MM-dd");
                            break;
                        case "ZBLB":
                            ModelList.ZBLB = node2.InnerText;
                            break;
                        case "GHLB":
                            switch (node2.InnerText)
                            {
                                case "1":
                                    ModelList.GHLB = "普通";
                                    break;
                                case "2":
                                    ModelList.GHLB = "专科";
                                    break;
                                case "3":
                                    ModelList.GHLB = "专家";
                                    break;
                                case "4":
                                    ModelList.GHLB = "急诊";
                                    break;
                                case "5":
                                    ModelList.GHLB = "特需";
                                    break;
                            }
                            break;
                        case "PBID":
                            ModelList.PBID = node2.InnerText;
                            break;
                        case "KSDM":
                            ModelList.KSDM = node2.InnerText;
                            break;
                        case "KSMC":
                            ModelList.KSMC = node2.InnerText;
                            break;
                        case "YSDM":
                            ModelList.YSDM = node2.InnerText;
                            break;
                        case "YSXM":
                            ModelList.YSXM = node2.InnerText;
                            break;
                        case "YSZC":
                            ModelList.YSZC = node2.InnerText;
                            break;
                        case "YSXB":
                            ModelList.YSXB = node2.InnerText;
                            break;
                        case "GHXE":
                            ModelList.GHXE = node2.InnerText;
                            break;
                        case "KGRS":
                            ModelList.KGRS = node2.InnerText;
                            break;
                        case "YGRS":
                            ModelList.YGRS = node2.InnerText;
                            break;
                        case "YYXE":
                            ModelList.YYXE = node2.InnerText;
                            break;
                        case "GHF":
                            ModelList.GHF = node2.InnerText;
                            break;
                        case "ZLF":
                            ModelList.ZLF = node2.InnerText;
                            break;
                        case "ZJF":
                            ModelList.ZJF = node2.InnerText;
                            break;
                        case "PBZT":
                            ModelList.PBZT = node2.InnerText;
                            break;
                        default:
                            break;

                    }
                }
                ModelList.GHF = (decimal.Parse(ModelList.GHF) + decimal.Parse(ModelList.ZLF)).ToString();
                kspb.Add(ModelList);


            }

            return kspb;
        }

        public string Yygh(KSPBResult model)
        {

            string result = "-1";
            try
            {
                string brid = string.Empty;
                brid = getBrxx(model.SFZH);
                if (string.IsNullOrEmpty(brid))
                {
                    BRXXResult brxx = new BRXXResult();
                    brxx.BRXM = model.BRXM;
                    brxx.SFZH = model.SFZH;
                    brxx.SJHM = model.SJHM;
                    brxx.BRXB = model.BRXB;

                    brid = regBrxx(brxx);
                }

                model.HYID = model.HYID.Trim();
                string zblb = model.ZBLB == "上午" ? "1" : "2";
                Random r = new Random();
                string pzhm = r.Next(10000000, 99999999).ToString();
                WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
                string strXml = "<?xml version='1.0' encoding='utf-8'?>";
                strXml += "<data>";
                strXml += "<funcode>100401</funcode>";
                strXml += "<KSDM>" + model.KSDM + "</KSDM>";
                strXml += "<YSDM>" + model.YSDM + "</YSDM>";
                strXml += "<BRID>" + brid + "</BRID>";
                strXml += "<YYLB>0</YYLB>";
                strXml += "<HYID>" + model.HYID + "</HYID>";
                strXml += "<ZBLB>" + zblb + "</ZBLB>";
                strXml += "<JZXH>" + model.JZXH + "</JZXH>";
                strXml += "<PBID>" + model.PBID + "</PBID>";
                strXml += "<YYRQ>" + model.GZRQ + "</YYRQ>";
                strXml += "<QHMM>" + pzhm + "</QHMM>";
                strXml += "<YYLY>3</YYLY>";
                strXml += "</data>";

                string outputXml = "";
                int refxml = hydservice.MainService(strXml, ref outputXml);

                if (refxml == 0)
                {
                    string out_trade_no = WxPayApi.GenerateOutTradeNo();

                    string sql = string.Format(@"insert into ms_yygh(out_trade_no,openid,brid,zblb,ksdm,ysdm,yyrq,hyid,pbid,pzhm,ghf) values('{0}','{1}','{2}','{3}','{4}','{5}',
                    '{6}','{7}','{8}','{9}','{10}')", out_trade_no, Session["openid"].ToString(), brid, zblb, model.KSDM, model.YSDM, model.GZRQ, model.HYID, model.PBID, pzhm, model.GHF);

                    int row = DbHelperSQL.ExecuteSql(sql);

                    result = model.HYID + "," + pzhm;

                    var data = NoticeTemplate("您好，您已预约挂号成功。", model.BRXM, model.GHLB, model.GZRQ + " " + model.ZBLB, model.KSMC, model.YSXM, "就诊当天请提前20分钟到达科室候诊，点击查看详情。");

                    sendMsg(Session["openid"].ToString(), "http://yygh.wdshun.cn/Yygh/YyDetail?hyid=" + model.HYID + "&pzhm=" + pzhm, bx_success, data);
                  

                }
                

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("yygh:yygh=" + ex.Message);
            }

            return result;
        }
        /// <summary>
        /// 获取病人id
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns>
        public string getBrxx(string sfzh)
        {
            string brid = string.Empty;

            WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
            string strXml = "<?xml version='1.0' encoding='utf-8'?>";
            strXml += "<data>";
            strXml += "<funcode>800002</funcode>";
            strXml += "<SFZH>" + sfzh + "</SFZH>";
            strXml += "<YYLY>3</YYLY>";
            strXml += "</data>";

            string outputXml = "";
            int refxml = hydservice.MainService(strXml, ref outputXml);

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(outputXml);
            XmlNodeList nodeList = xDoc.SelectSingleNode("data//list").ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                BRXXResult ModelList = new BRXXResult();
                XmlNodeList nodeList2 = node.ChildNodes;
                foreach (XmlNode node2 in nodeList2)
                {
                    switch (node2.Name)
                    {
                        case "BRID":
                            brid = node2.InnerText;
                            break;
                    }
                }

            }

            return brid;
        }

        /// <summary>
        /// 注册病人信息
        /// </summary>
        /// <returns></returns>
        public string regBrxx(BRXXResult model)
        {

            string brid = string.Empty; ;

            string sex = model.BRXB == "1" ? "男" : "女";

            WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
            string strXml = "<?xml version='1.0' encoding='utf-8'?>";
            strXml += "<data>";
            strXml += "<funcode>100301</funcode>";
            strXml += "<SFZH>" + model.SFZH + "</SFZH>";
            strXml += "<ZJLB>1</ZJLB>";
            strXml += "<BRXM>" + model.BRXM + "</BRXM>";
            strXml += "<BRXB>" + sex + "</BRXB>";
            strXml += "<SJHM>" + model.SJHM + "</SJHM>";
            strXml += "<JTDZ>" + "" + "</JTDZ>";
            strXml += "<YBKH>" + "" + "</YBKH>";
            strXml += "<YBLB>" + "" + "</YBLB>";
            strXml += "<YYLY>3</YYLY>";
            strXml += "</data>";

            string outputXml = "";
            int refxml = hydservice.MainService(strXml, ref outputXml);

            if (refxml == 0)
            {

                brid = getBrxx(model.SFZH);
            }
            return brid;
        }
        /// <summary>
        /// 获取预约信息
        /// </summary>
        /// <returns></returns>
        public List<YYXXModel> getyyxx(string sfzh)
        {
            WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
            string strXml = "<?xml version='1.0' encoding='utf-8'?>";
            strXml += "<data>";
            strXml += "<funcode>800003</funcode>";
            strXml += "<SFZH>" + sfzh + "</SFZH>";
            strXml += "<YYLY>1,3,7</YYLY>";
            strXml += "</data>";

            string outputXml = "";
            int refxml = hydservice.MainService(strXml, ref outputXml);

            List<YYXXModel> yyxx1 = new List<YYXXModel>();


            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(outputXml);
            XmlNodeList nodeList = xDoc.SelectSingleNode("data//list").ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                YYXXModel ModelList = new YYXXModel();
                XmlNodeList nodeList2 = node.ChildNodes;
                foreach (XmlNode node2 in nodeList2)
                {
                    switch (node2.Name)
                    {
                        case "HYID":
                            ModelList.HYID = node2.InnerText;
                            break;
                        case "YYXH":
                            ModelList.YYXH = node2.InnerText;
                            break;
                        case "BRID":
                            ModelList.BRID = node2.InnerText;
                            break;
                        case "KSDM":
                            ModelList.KSDM = node2.InnerText;
                            break;
                        case "KSMC":
                            ModelList.KSMC = node2.InnerText;
                            break;
                        case "YGXM":
                            ModelList.YGXM = node2.InnerText;
                            break;
                        case "JZSJ":
                            ModelList.JZSJ = node2.InnerText;
                            break;
                        case "YYRQ":
                            ModelList.YYRQ = DateTime.Parse(node2.InnerText).ToString("yyyy-MM-dd");
                            break;
                        case "PZHM":
                            ModelList.PZHM = node2.InnerText;
                            break;
                        case "ZBLB":
                            ModelList.ZBLB = node2.InnerText == "1" ? "上午" : "下午";
                            break;
                        case "JZXH":
                            ModelList.JZXH = node2.InnerText;
                            break;
                        case "YYBZ":
                            switch (node2.InnerText)
                            {
                                case "0":
                                    ModelList.YYBZ = "待取号";
                                    ModelList.YY = "等待您取号";
                                    break;
                                case "1":
                                    ModelList.YYBZ = "已取号";
                                    ModelList.YY = "您已在医院取号,感谢来我院按时就诊";
                                    break;
                                case "2":
                                    ModelList.YYBZ = "已作废";
                                    ModelList.YY = "您未在约定时间取号,系统将作废该订单";
                                    break;
                                case "3":
                                    ModelList.YYBZ = "已取消";
                                    ModelList.YY = "因自身原因,主动取消预约";
                                    break;
                                case "4":
                                    ModelList.YYBZ = "退号";
                                    ModelList.YY = "因自身原因,已退号";
                                    break;
                            }

                            break;
                        case "XYJF":
                            ModelList.XYJF = node2.InnerText;
                            break;
                        case "BRXM":
                            ModelList.BRXM = node2.InnerText;
                            break;
                        case "SFZH":
                            ModelList.SFZH = node2.InnerText;
                            break;
                        case "GHLB":
                            switch (node2.InnerText)
                            {
                                case "1":
                                    ModelList.GHLB = "普通";
                                    break;
                                case "2":
                                    ModelList.GHLB = "专科";
                                    break;
                                case "3":
                                    ModelList.GHLB = "专家";
                                    break;
                                case "4":
                                    ModelList.GHLB = "急诊";
                                    break;
                                case "5":
                                    ModelList.GHLB = "特需";
                                    break;
                            }
                            break;
                        case "HMD":
                            ModelList.HMD = node2.InnerText;
                            break;
                        case "GHF":
                            ModelList.GHF = node2.InnerText;
                            break;
                        default:
                            break;

                    }
                }
                yyxx1.Add(ModelList);
            }

            return yyxx1;
        }

        public string CancelYy(YYXXModel model)
        {
            string result = "0";
            try
            {

                WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference1.ZJSXHYYServiceSoapClient();
                string strXml = "<?xml version='1.0' encoding='utf-8'?>";
                strXml += "<data>";
                strXml += "<funcode>800004</funcode>";
                strXml += "<BRID>" + model.BRID + "</BRID>";
                strXml += "<QHMM>" + model.PZHM + "</QHMM>";
                strXml += "<YYRQ>" + DateTime.Parse(model.YYRQ).ToString("yyyy/MM/dd") + "</YYRQ>";
                strXml += "<YYLY>3</YYLY>";
                strXml += "</data>";

                string outputXml = "";
                int refxml = hydservice.MainService(strXml, ref outputXml);

                result = refxml.ToString();

                if (refxml == 0) {
                    var data = NoticeTemplate("您好，您的挂号单已成功取消。", model.BRXM, model.KSMC, model.YGXM, model.YYRQ+" "+model.JZSJ, model.DDHM, "取消预约挂号成功。");

                    sendMsg(Session["openid"].ToString(), "http://yygh.wdshun.cn/Yygh/YyDetail?hyid=" + model.HYID + "&pzhm=" + model.PZHM, bx_cancel, data);
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

        public string RefundFee(string out_trade_no)
        {
            LogHelper.WriteLog("RefundFee:start");
            string result = "0";

            try {
                string sql = string.Format("select * from ms_yygh where out_trade_no='{0}'", out_trade_no);

                DataTable dt = DbHelperSQL.Query(sql).Tables[0];



                if (dt.Rows.Count > 0)
                {

                    string outrefundno = WxPayApi.GenerateOutTradeNo();

                    WxYygh.ServiceReference2.ZJSXHYYServiceSoapClient hydservice = new WxYygh.ServiceReference2.ZJSXHYYServiceSoapClient();
                    string strXml = "<?xml version='1.0' encoding='utf-8'?>";

                    strXml = "<?xml version='1.0' encoding='utf-8'?>";
                    strXml += "<data>";
                    strXml += "<funcode>500004</funcode>";
                    strXml += "<hyid>" + dt.Rows[0]["hyid"].ToString() + "</hyid>";
                    strXml += "<outrefundno>" + outrefundno + "</outrefundno>";

                    strXml += "</data>";

                    string outputXml = "";
                    int refxml = hydservice.MainService(strXml, ref outputXml);

                    if (refxml == 0)
                    {

                        WxPayData model = new WxPayData();
                        model.SetValue("out_trade_no", out_trade_no);
                        model.SetValue("out_refund_no", outrefundno);
                        model.SetValue("total_fee", int.Parse((100 * double.Parse(dt.Rows[0]["ghf"].ToString())).ToString()));
                        model.SetValue("refund_fee", int.Parse((100 * double.Parse(dt.Rows[0]["ghf"].ToString())).ToString()));
                        //model.SetValue("op_user_id", Session["openid"].ToString());

                        WxPayData res = WxPayApi.Refund(model);
                        if (res.GetValue("return_code").ToString() != "SUCCESS")
                        {
                            result = res.GetValue("return_msg").ToString();
                        }



                    }
                    else
                    {
                        result = outputXml;
                    }

                }
                else
                {
                    result = "退费失败";
                }
            }
            catch (Exception ex) {

                LogHelper.WriteLog("RefundFee:" + ex.Message);
                result = ex.Message;
            }
           

            

            return result;
        }

        #region 微信发送消息
       
        public void sendMsg(string touser, string href,string template_id, object data)
        {

            string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", WeChatBase.GetAccessToken());

            var msg = new TemplateMessage
            {
                template_id = template_id,
                touser = touser,
                url = href,
                data = data
            };


            string json = JsonHelper.GetJson(msg);

            string responseText = HttpHelper.Instance.post(url, json);

            LogHelper.WriteLog("json=" + json);
            LogHelper.WriteLog("responseText=" + responseText);
        }

        public static object NoticeTemplate(string first, string keyword1, string keyword2, string keyword3, string keyword4, string keyword5, string remark)
        {
            var data = new
            {
                first = new TemplateDataItem(first, "#3990eb"),
                keyword1 = new TemplateDataItem(keyword1, "#feb91a"),
                keyword2 = new TemplateDataItem(keyword2, "#feb91a"),
                keyword3 = new TemplateDataItem(keyword3, "#feb91a"),
                keyword4 = new TemplateDataItem(keyword4, "#000000"),
                keyword5 = new TemplateDataItem(keyword5, "#000000"),
                remark = new TemplateDataItem(remark, "#fe5627")
            };
            return data;
        }
        #endregion
    }
}